package com.bosssoft.ct.springcloud.service.impl;

import com.bosssoft.ct.springcloud.dao.UserDao;
import com.bosssoft.ct.springcloud.exception.BusinessException;
import com.bosssoft.ct.springcloud.exception.BusinessExceptionCode;
import com.bosssoft.ct.springcloud.pojo.dto.UserDTO;
import com.bosssoft.ct.springcloud.pojo.entity.User;
import com.bosssoft.ct.springcloud.pojo.query.UserQuery;
import com.bosssoft.ct.springcloud.pojo.vo.UserVO;
import com.bosssoft.ct.springcloud.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ChenTong
 * @version 1.0
 * @description
 * @date 2020/6/21 20:12
 * @modified
 **/
@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Resource
    private UserDao userDao;

    /**
     * 用户删除
     * @author ChenTong
     * @param id
     * @return int
     * @date 2020/6/22 7:20
     */
    @Override
    @CacheEvict(cacheNames = {"userList"})
    public int delete(Long id) {
        try {
            return userDao.delete(id);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            throw new BusinessException(BusinessExceptionCode.DATA_OPERATE_FAIL,e);
        }
    }

    /**
     * 用户更新
     * @author ChenTong
     * @param dto
     * @return int
     * @date 2020/6/22 7:20
     */
    @Override
    @CacheEvict(cacheNames = {"userList"})
    public int update(UserDTO dto) {
        try {
            User user = dto.dtoToEntity();
            return userDao.update(user);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            throw new BusinessException(BusinessExceptionCode.DATA_OPERATE_FAIL,e);
        }
    }

    /**
     * 插入数据
     * @param dto
     * @return int
     * @author ChenTong
     * @date 2020/6/22 8:17
     */
    @Override
    @CacheEvict(cacheNames = {"userList"})
    public int insert(UserDTO dto) {
        try {
            User user = dto.dtoToEntity();
            log.info("service insert:{}",user);
            return userDao.insert(user);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            throw new BusinessException(BusinessExceptionCode.DATA_OPERATE_FAIL,e);
        }

    }

    /**
     * 用户查询
     * @author ChenTong
     * @param query
     * @return java.util.List<com.bosssoft.ct.springcloud.pojo.vo.UserVO>
     * @date 2020/6/22 7:20
     */
    @Cacheable(cacheNames = {"userList"})
    @Override
    public List<UserVO> selectByCondition(UserQuery query) {
        try {
            List<User> users =  userDao.selectByCondition(query);
            List<UserVO> userVOS = new ArrayList<>();
            for (User user: users
            ) {
                UserVO userVO = new UserVO();
                userVO.entityToVO(user);
                userVOS.add(userVO);
            }
            return userVOS;
        }catch (Exception e){
            log.error(e.getMessage(),e);
            throw new BusinessException(BusinessExceptionCode.DATA_OPERATE_FAIL,e);
        }

    }

    /**
     *
     * @author ChenTong
     * @param id
     * @return int
     * @date 2020/6/22 7:21
     */

    /**
     * @Override
    public UserVO selectByPrimaryKey(Long id) {
        try {
            UserQuery query = new UserQuery();
            query.setId(id);
            User user = userDao.selectByCondition(query).get(0);
            UserVO userVO = new UserVO();
            userVO.entityToVO(user);
            return userVO;
        }catch (Exception e){
            log.error(e.getMessage(),e);
            throw new BusinessException(BusinessExceptionCode.DATA_OPERATE_FAIL,e);
        }

    }**/



    /**
     * 用户登录业务接口
     * @param userDTO UserDTO,用户数据传输对象
     * @return boolean
     * @author ChenTong
     * @date 2020/6/22 7:14
     */
    @Override
    public UserVO login(UserDTO userDTO) {
        if (null == userDTO || userDTO.getUsername()==null||userDTO.getPassword()==null) return null;
        try {
            // 获取数据库中实际数据
            UserQuery query = new UserQuery();
            query.setName(userDTO.getUsername());
            User dbUser = userDao.selectByCondition(query).get(0);
            if (userDTO.getPassword().equals(dbUser.getUsername())){
                UserVO userVO = new UserVO();
                userVO.entityToVO(dbUser);
                return userVO;
            }
            return null;

        }catch (Exception e){
            log.error(e.getMessage(),e);
            throw new BusinessException(BusinessExceptionCode.LOGIN_FAIL,e);
        }

    }

}
